package middleware

import (
	"context"
	"datum_user/controller"
	"datum_user/grpc"
	"datum_user/proto"
	"github.com/gin-gonic/gin"
)

func AuthUser() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.Request.Header.Get("token")
		if token == "" {
			controller.ToJson(c,200,"access fail,don't find token",nil)
			c.Abort()
			return
		}
		tokenReq := proto.ParseTokenRequest{Token: token}
		res,err := grpc.GetTokenService().ParseToken(context.Background(),&tokenReq)
		if err != nil {
			controller.ToJson(c,200,"token parse fail",nil)
			c.Abort()
			return
		}else {
			c.Set("uid", res.Uid)
			c.Next()
		}
	}
}

